package cn.tedu.clyback.role.controller;


import cn.tedu.clyback.base.response.JsonResult;
import cn.tedu.clyback.role.pojo.dto.InsertPermissionParam;
import cn.tedu.clyback.role.pojo.dto.RoleSaveParam;
import cn.tedu.clyback.role.pojo.dto.RoleSelectParam;
import cn.tedu.clyback.role.pojo.vo.RoleVO;
import cn.tedu.clyback.role.pojo.vo.SelectUserByRoleIdVo;
import cn.tedu.clyback.role.service.RoleService;
import cn.tedu.clyback.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/role")
public class RoleController {
    @Autowired
    RoleService roleService;

    @GetMapping("/select")
    public JsonResult selectRole( RoleSelectParam roleSelectParam){
        log.debug("开始处理【查询角色】的请求，参数：{}", roleSelectParam);
        List<RoleVO> list = roleService.selectRole(roleSelectParam);
        log.debug("查询结果：{}", list);
        return JsonResult.ok(list);
    }

    @PostMapping("/save")
    public JsonResult saveRole(RoleSaveParam roleSaveParam){
        log.debug("开始处理【保存角色】的请求，参数：{}", roleSaveParam);
        roleService.saveRole(roleSaveParam);
        log.debug("保存成功");
        return JsonResult.ok();
    }

    @PostMapping("/update/status/{roleId}/{status}")
    public JsonResult updateStatus(@PathVariable Integer roleId,@PathVariable String status){
        log.debug("开始处理【更新用户状态】的请求，参数：{}", roleId,status);
        roleService.updateStatus(roleId,status);
        return JsonResult.ok();
    }
    @PostMapping("/delete/{roleId}")
    public JsonResult deleteRole(@PathVariable Integer roleId){
        log.debug("开始处理【删除角色】的请求，参数：{}", roleId);
        roleService.deleteRole(roleId);
        return JsonResult.ok();
    }
    @GetMapping("/selectuser/{roleId}")
    public JsonResult selectUserByRoleId(@PathVariable Integer roleId){
        log.debug("开始处理【根据角色id查询用户】的请求，参数：{}", roleId);
        List<SelectUserByRoleIdVo> list=roleService.selectUserByRoleId(roleId);
        return JsonResult.ok(list);
    }

    @PostMapping("/deleteuser/{roleId}/{userId}")
    public JsonResult deleteUserRole( @PathVariable Integer roleId,@PathVariable Long userId){
        log.debug("开始处理【根据角色id删除用户】的请求，参数：{} ,{}", roleId,userId);
        roleService.deleteUserRole(roleId,userId);
        return JsonResult.ok();
    }
    @GetMapping("/selectadduser/{roleId}")
    public JsonResult selectAddUserByRoleId(@PathVariable Integer roleId){
        log.debug("开始处理【根据角色id查询用户】的请求，参数：{}", roleId);
        List<SelectUserByRoleIdVo> list=roleService.selectUser(roleId);
        return JsonResult.ok(list);
    }
    @PostMapping("/adduserrole/{roleId}/{userId}")
    public JsonResult addUserRole( @PathVariable Integer roleId,@PathVariable Long userId){
        log.debug("开始处理【根据角色id添加用户】的请求，参数：{} ,{}", roleId,userId);
        roleService.addUserRole(roleId,userId);
        return JsonResult.ok();
    }

    @GetMapping("/selectuserrole/{userId}")
    public JsonResult selectUserRole(@PathVariable Long userId){
        log.debug("开始处理【根据用户id查询已绑定角色】的请求，参数：{}", userId);
        List<RoleVO> list=roleService.selectUserRole(userId);
        return JsonResult.ok(list);
    }

    @PostMapping("/removeuserrole/{roleId}/{userId}")
    public JsonResult removeUserRole(@PathVariable Integer roleId ,@PathVariable Long userId){
        log.debug("开始处理【根据角色id移除用户角色】的请求，参数：{}", roleId);
        roleService.removeUserRole(roleId,userId);
        return JsonResult.ok();
    }

    @GetMapping("/selectunadduserrole/{userId}")
    public JsonResult selectUnAddUserRole(@PathVariable Long userId){
        log.debug("开始处理【根据用户id查询未绑定角色】的请求，参数：{}", userId);
        List<RoleVO> list=roleService.selectUnAddUserRole(userId);
        return JsonResult.ok(list);
    }


}